package com.moefor.clover.service;

import java.util.Date;
import java.util.Set;

import javax.annotation.Resource;

import org.hibernate.Hibernate;
import org.springframework.stereotype.Service;

import com.moefor.clover.dao.*;
import com.moefor.clover.model.Comment;
import com.moefor.clover.model.Goods;
import com.moefor.clover.model.Market;
import com.moefor.clover.model.Order;
import com.moefor.clover.model.User;
import com.moefor.clover.model.element.Picture;
import com.moefor.clover.model.enums.StateOfGoods;
import com.moefor.clover.model.enums.StateOfOrder;
import com.moefor.clover.util.MessagePackager;

@Service
public class ShopService {
	@Resource
	private GoodsDao goodsDao;
	
	@Resource
	private MarketDao marketDao;
	
	@Resource
	private OrderDao orderDao;
	
	@Resource
	private CommunicationService communicationService;
	
	public MessagePackager searchMarket(String keyword){
		MessagePackager msgPack = new MessagePackager(false, null, "未知错误");
		msgPack.setData(marketDao.search(keyword));
		if(msgPack.getData() == null){
			msgPack.setData("找不到符合条件市场!");
		}else{
			msgPack.setState(true);
		}
		return msgPack;
	}
	
	public MessagePackager getMarket(int marketId){
		MessagePackager msgPack = new MessagePackager(false, null, "未知错误!");
		msgPack.setData(marketDao.get(marketId));
		if(msgPack.getData() == null){
			msgPack.setData("市场不存在");
		}else{
			msgPack.setState(true);
		}
		return msgPack;
	}
	
	public MessagePackager addGoods(User user, String goodsName, String goodsIntroduction, float goodsPrice, Set<Market> marketItems, Set<Picture> picItems){
		MessagePackager msgPack = new MessagePackager();
		Goods goods = new Goods();
		goods.setOwner(user);
		goods.setName(goodsName);
		goods.setPrice(goodsPrice);
		goods.setIntroduction(goodsIntroduction);
		goods.setMarketItems(marketItems);
		goods.setPicItems(picItems);
		goods.setDate(new Date());
		goods.setState(StateOfGoods.ON_SALE);
		System.out.println("OK Goods");
		if(goodsDao.add(goods)){
			msgPack.setState(true);
			msgPack.setData("商品发布成功!");
		}else{
			msgPack.setState(false);
			msgPack.setData("商品发布失败!");
		}
		return msgPack;
	}
	
	public MessagePackager bookGoods(User user, int goodsId, String remarks){
		MessagePackager msgPack = new MessagePackager(false, null, "未知错误.");
		Goods g = goodsDao.get(goodsId);
		if(g==null){
			msgPack.setData("商品不存在");
			return msgPack;
		}
		if(user.getId() == g.getOwner().getId()){
			msgPack.setData("请不要自娱自乐哦~");
			return msgPack;
		}
		if(remarks.equals("")){
			msgPack.setData("留言不可为空");
			return msgPack;
		}
		if(!g.getState().equals(StateOfGoods.ON_SALE)){
			msgPack.setData("商品不是可交易状态!");
			return msgPack;
		}
		Order o = orderDao.get(goodsId, user.getId());
		if(o != null && (o.getState().equals(StateOfOrder.WAIT_FOR_CFMD))){
			msgPack.setData("您已经预定了此订单");
			return msgPack;
		}
		o = new Order();
		o.setGoods(g);
		o.setRemarks(remarks);
		o.setCustomer(user);
		o.setDate(new Date());
		o.setPrice(g.getPrice());
		o.setState(StateOfOrder.WAIT_FOR_CFMD);
		if(orderDao.add(o)){
			communicationService.sendMessage(user, g.getOwner().getId(), "亲,你好!我已经预定了您发布的商品:" + g.getName() + " .请尽快确认哦~");
			msgPack.setState(true);
			msgPack.setData("预定成功!");
		}else{
			msgPack.setData("预定失败,请稍后再试!");
		}
		return msgPack;
	}
	
	public MessagePackager banGoods(User user, int goodsId){
		MessagePackager msgPack = new MessagePackager(false, null, "未知错误.");
		Goods g = goodsDao.get(goodsId);
		if(g == null){
			msgPack.setData("商品不存在");
			return msgPack;
		}
		if(g.getOwner().getId() != user.getId()){
			msgPack.setData("您不是该商品的所有者, 无权进行下架操作.");
			return msgPack;
		}
		g.setState(StateOfGoods.BAN);
		if(goodsDao.update(g)){
			orderDao.updStateByGoods(g.getId(), StateOfOrder.FINISHED);
			msgPack.setState(true);
			msgPack.setData("商品下架成功!");
		}else{
			msgPack.setState(false);
			msgPack.setData("商品下架失败!");
		}
		return msgPack;
	}
	
	/*
	public MessagePackager cfmdOrder(User user, int orderId){
		MessagePackager msgPack = new MessagePackager(false, null, "未知错误.");
		Order o = orderDao.get(orderId);
		if(o == null){
			msgPack.setData("订单不存在");
			return msgPack;
		}
		if(o.getGoods().getOwner().getId() != user.getId()){
			msgPack.setData("您不是该订单的卖家, 无权进行操作!");
			return msgPack;
		}
		if(o.getState().equals(StateOfOrder.WAIT_FOR_CFMD)){
			msgPack.setData("该订单的不处于待确认状态!");
			return msgPack;
		}
		
		if(orderDao.update(o)){
			msgPack.setState(true);
			msgPack.setData("订单确认成功,请联系买家进行线下交易!");
			communicationService.sendMessage(user, o.getCustomer().getId(), "亲,你好!我已经确认了您想要购买的商品:" + o.getGoods().getName() + " ,请尽快联系我,进行线下交易哦~");
		}else{
			msgPack.setState(false);
			msgPack.setData("失败!");
		}
		return msgPack;	
	}
	*/
	/*
	public MessagePackager rejectOrder(User user, int orderId){
		MessagePackager msgPack = new MessagePackager(false, null, "未知错误.");
		Order o = orderDao.get(orderId);
		if(o == null){
			msgPack.setData("订单不存在");
			return msgPack;
		}
		if(o.getGoods().getOwner().getId() != user.getId()){
			msgPack.setData("您不是该订单的卖家, 无权进行操作!");
			return msgPack;
		}
		if(o.getState().equals(StateOfOrder.WAIT_FOR_CFMD)){
			msgPack.setData("该订单的不处于待确认状态!");
			return msgPack;
		}
		o.setState(StateOfOrder.CLOSED);
		if(orderDao.update(o)){
			msgPack.setState(true);
			msgPack.setData("订单拒绝成功,请联系买家进行线下交易!");
			communicationService.sendMessage(user, o.getCustomer().getId(), "亲,你好!抱歉,我这款商品:" + o.getGoods().getName() + " ,暂时不出手~");
			
		}else{
			msgPack.setState(false);
			msgPack.setData("失败!");
		}
		return msgPack;	
	}*/
	
	public MessagePackager finishedOrder(User user, int goodsId){
		MessagePackager msgPack = new MessagePackager(false, null, "未知错误.");
		Goods g = goodsDao.get(goodsId);
		if(g == null){
			msgPack.setData("订单不存在");
			return msgPack;
		}
		if(g.getOwner().getId() != user.getId()){
			msgPack.setData("您不是该商品的卖家, 无权进行操作!");
			return msgPack;
		}
		Set<Order> oSet = g.getOrderItems();
		boolean flage = false;
		for(Order o : oSet){
			if(o.getState().equals(StateOfOrder.WAIT_FOR_CFMD)){
				flage = true;
				break;
			}
		}
		if(!flage){
			msgPack.setData("该商品未被预定!");
			return msgPack;
		}
		for(Order o : oSet){
			o.setState(StateOfOrder.FINISHED);
		}
		g.setOrderItems(oSet);
		g.setState(StateOfGoods.SOLD);
		if(goodsDao.update(g)){
			msgPack.setState(true);
			msgPack.setData("交易确认成功!");
		}else{
			msgPack.setState(false);
			msgPack.setData("交易确认失败!");
		}
		return msgPack;
		/*
		MessagePackager msgPack = new MessagePackager(false, null, "未知错误.");
		Order o = orderDao.get(orderId);
		if(o == null){
			msgPack.setData("订单不存在");
			return msgPack;
		}
		if(o.getGoods().getOwner().getId() != user.getId()){
			msgPack.setData("您不是该订单的卖家, 无权进行操作!");
			return msgPack;
		}
		if(o.getState().equals(StateOfOrder.WAIT_FOR_CFMD)){
			msgPack.setData("该订单的不处于正在交易状态!");
			return msgPack;
		}
		
		Goods g = o.getGoods();
		g.setState(StateOfGoods.SOLD);
		if(orderDao.updStateByGoods(g.getId(), StateOfOrder.FINISHED)>0 && goodsDao.update(g)){
			msgPack.setState(true);
			msgPack.setData("订单确认成功!");
			communicationService.sendMessage(user, o.getCustomer().getId(), "亲,你好!我已经确认从您那购买的商品:" + o.getGoods().getName() + " ,合作愉快~");
		}else{
			msgPack.setState(false);
			msgPack.setData("订单确认失败!");
		}
		return msgPack;	
		*/
	}
	
	public MessagePackager searchGoods(String keyword, int pageNum, int pageSize, Set<Market> marketSet){
		MessagePackager msgPack = new MessagePackager(false, null, "未知错误!");
		msgPack.setData(goodsDao.search(keyword, pageNum, pageSize, marketSet, StateOfGoods.ON_SALE));
		if(msgPack.getData() == null){
			msgPack.setData("该区域没有更多的商品了!");
			return msgPack;
		}
		msgPack.setState(true);
		return msgPack;
	}
	
	public MessagePackager searchGoodsByMakret(String marketName, int pageNum, int pageSize){
		MessagePackager msgPack = new MessagePackager(false, null, "未知错误!");
		msgPack.setData(goodsDao.searchByMarket(marketName, pageNum, pageSize, StateOfGoods.ON_SALE));
		if(msgPack.getData() == null){
			msgPack.setData("暂无商品!");
			return msgPack;
		}
		msgPack.setState(true);
		return msgPack;
	}
	
	public MessagePackager searchGoodsByMakret(Integer marketId, Integer pageNum, Integer pageSize){
		MessagePackager msgPack = new MessagePackager(false, null, "未知错误!");
		msgPack.setData(goodsDao.searchByMarket(marketId, pageNum, pageSize, StateOfGoods.ON_SALE));
		if(msgPack.getData() == null){
			msgPack.setData("暂无商品!");
			return msgPack;
		}
		msgPack.setState(true);
		return msgPack;
	}
	 
	public MessagePackager getGoods(int goodsId){
		MessagePackager msgPack = new MessagePackager(false, null, "未知错误!");
		msgPack.setData(goodsDao.get(goodsId));
		if(msgPack.getData() != null){
			msgPack.setState(true);
		}else{
			msgPack.setData("商品不存在");
		}
		return msgPack;
	}
	
	public MessagePackager getGoodsByOwnerId(int ownerId){
		MessagePackager msgPack = new MessagePackager(false, null, "未知错误!");
		msgPack.setData(goodsDao.getByOwner(ownerId));
		if(msgPack.getData() != null){
			msgPack.setState(true);
		}else{
			msgPack.setData("该用户无商品");
		}
		return msgPack;
	}
	
	public MessagePackager getAroundMarket(Double X, Double Y){
		MessagePackager msgPack = new MessagePackager();
		msgPack.setData(marketDao.getAround(X, Y, 0.1, 0.2));
		if(msgPack.getData() == null){
			msgPack.setState(false);
			msgPack.setData("该区域暂无市场!");
			return msgPack;
		}
		msgPack.setState(true);
		return msgPack;
	}
	
	public MessagePackager getOrderByCustomer(int customerId, StateOfOrder state){
		MessagePackager msgPack = new MessagePackager(false, null, "未知错误！");
		msgPack.setData(orderDao.getByCustomer(customerId, state));
		if(msgPack.getData() == null){
			msgPack.setData("暂无订单!");
		}else{
			msgPack.setState(true);
		}
		return msgPack;
	}
	
	public MessagePackager getOrderByOwner(int ownerId, StateOfOrder state){
		MessagePackager msgPack = new MessagePackager(false, null, "未知错误！");
		msgPack.setData(orderDao.getByOwner(ownerId, state));
		if(msgPack.getData() == null){
			msgPack.setData("暂无订单!");
		}else{
			msgPack.setState(true);
		}
		return msgPack;
	}
}
